package zzxkj.blog.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import zzxkj.blog.WorkingException.NotFoundException;
import zzxkj.blog.dto.DetailedBlog;
import zzxkj.blog.dto.FirstPageBlog;
import zzxkj.blog.cacheService.RankListService;
import zzxkj.blog.service.*;
import zzxkj.blog.utils.MarkdownUtil;
import zzxkj.blog.utils.RedisUtil;

import java.util.List;

/**
 * @author zzxkj
 */
@Slf4j
@Controller
public class IndexConntroller {
    private final BlogService blogService;
    private final SearchService searchService;
    private final RankListService rankListService;
    private final RedisUtil redisUtil;
    private final ModelMapper modelMapper;

    public IndexConntroller(BlogService blogService, SearchService searchService, RankListService rankListService, RedisUtil redisUtil, ModelMapper modelMapper) {
        this.blogService = blogService;
        this.searchService = searchService;
        this.rankListService = rankListService;
        this.redisUtil = redisUtil;
        this.modelMapper = modelMapper;
    }

    @GetMapping("/")
    public String index(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, Model model) {
        int size = 8;
        String sort = "update_time"+" DESC";
        PageHelper.startPage(pageNum,size);
        List<FirstPageBlog> blogs = blogService.getAllFirstPageBlog();
        PageInfo<FirstPageBlog> page = new PageInfo<FirstPageBlog>(blogs);
        model.addAttribute("totalElements",blogService.countBlog());
        model.addAttribute("page", page);
        //传入排行榜
        model.addAttribute("rankBlogs", rankListService.getWeekRank());
        model.addAttribute("recommendBlogs", blogService.listRecommendBlogTop(8));
        return "index";
    }

    /**
     * 获取周排行榜
     * @param model
     * @return
     */
    @GetMapping("/weekRank")
    public String weekRank(Model model){
        //传入排行榜
        model.addAttribute("rankBlogs", rankListService.getWeekRank());
        return "index :: weekRank";
    }

    /**
     * 获取总排行榜
     * @param model
     * @return
     */
    @GetMapping("/allRank")
    public String allRank(Model model){
        //传入排行榜
        model.addAttribute("rankBlogs", rankListService.getAllRank());
        return "index :: weekRank";
    }
    @PostMapping("/search")
    public String search(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,  @RequestParam String query, Model model) {
        int size = 8;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(size);
        PageInfo page = searchService.search(pageInfo,query);
        System.out.println(page.getTotal()+"    "+page.getList().size());
        model.addAttribute("page", page);
        model.addAttribute("query", query);
        model.addAttribute("totalElements",page.getTotal());
        return "search";
    }

    /**
     * 这里使用Redis进行缓存
     * json——object
     * Cache-Aside Pattern
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        DetailedBlog blog =  blogService.getBlogDetail(id);
        if (blog == null) {
            throw new NotFoundException("此篇博客不存在，或被移除");
        }
        //MarkDown文本转成HTML文本
        String HTMLContent = MarkdownUtil.markdownToHtmlExtensitons(blog.getContent());
        blog.setContent(HTMLContent);
        model.addAttribute("blog", blog);
        return "blog";
    }
/*    @GetMapping("/footer/newblog")
    public String newblogs(Model model) {
        model.addAttribute("newblogs", blogService.listRecommendBlogTop(3));
        return "fragments  :: newblogList";
    }*/
}
